<?php
namespace app\controller;

use app\BaseController;
use app\model\User;
use think\facade\Request;
use think\facade\Db;

class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello()
    {
        return 'sss';
    }

    //用户登陆的方法
    public function login()
    {
        $username = Request::param('username');
        $password = Request::param('password');

        $userModel = new User();
        $user = $userModel->checkLogin($username, $password);


        if ($user) {

            if(!$userModel->checkSwitch($user->user_root,$user->user_switch))
            {
                //用户被禁用
                return json(['code' => 201, 'msg' => '用户已被禁用', ]);
            }

            //检查是否被软删除
            if($user->user_deleted==1)
            {
                return json(['code' => 403, 'msg' => '用户已被删除', ]);
            }
            //登陆成功插入sessionid
            $sessionID = md5(uniqid(rand(), true));
            Db::name('user')->where('user_id', $user->user_id)->update(['user_sessionID' => $sessionID]);
            $user = $userModel->checkLogin($username, $password);


            // 登录成功，返回JSON响应，状态码200,并且签发token
            return json(['code' => 200, 'msg' => '登录成功', 'data' =>['token'=> signToken($user)]]);
        } else {
            // 登录失败，返回JSON响应，状态码可以根据需要自定义，这里使用401
            return json(['code' => 403, 'msg' => '用户名或密码错误']);
        }






    }
}
